#include "qkasakorekcija.h"
#include <QItemSelectionModel>
#include <QSqlQuery>
#include <QMessageBox>

QKasaKorekcija::QKasaKorekcija(QWidget *parent)
	: QMyBaseFormWidget(parent)
	,selRow(-1)
	,textId("")
{
	ui.setupUi(this);
	ui.pushButton->setFocus();
	model = new QStandardItemModel(0,4);
	model->setHeaderData( 0, Qt::Horizontal, trUtf8("Шифра"));
	model->setHeaderData( 1, Qt::Horizontal, trUtf8("Артикал"));
	model->setHeaderData( 2, Qt::Horizontal, trUtf8("Едм"));
	model->setHeaderData( 3, Qt::Horizontal, trUtf8("Влезна количина"));

	ui.tableView->setModel(model);
	header = new QHeaderView(Qt::Horizontal, this);
	ui.tableView->setHorizontalHeader(header);
	header->show();
}

QKasaKorekcija::~QKasaKorekcija()
{

}

void QKasaKorekcija::pressEscape()
{
	emit closeW();
}

void QKasaKorekcija::on_pushButton_clicked()
{
	emit scallKomintenti((QWidget*)this);
}

void QKasaKorekcija::setFocusLine()
{
	ui.pushButton->setFocus();
}
void QKasaKorekcija::setKomintent(QString text, QString textName)
{
	ui.lineEdit->setText(textName);

}

void QKasaKorekcija::setArtikli(QString text, QString textName)
{
	ui.lineEdit_2->setText(textName);
	textId = text;
}

void QKasaKorekcija::setFocusLine2()
{
	ui.pushButton_2->setFocus();
}

void QKasaKorekcija::on_pushButton_2_clicked()
{
	emit scallArtikli((QWidget*)this);
}

void QKasaKorekcija::on_pushButton_3_clicked()
{
	if(textId.toInt() == -1)
	{
		return;
	}

	QSqlQuery query;
	query.prepare("select sifra, artikal, edm from artikli where id = :id");
	query.bindValue(":id", textId.toInt());	
	query.exec();
	int  r= query.numRowsAffected();
	query.next();	
	int row = model->rowCount();

	QStandardItem *item = new QStandardItem(query.value(0).toString());
	item->setEditable(false);
	ui.tableView->setRowHeight(row, 18);
	model->setItem(row, 0, item);

	item = new QStandardItem(query.value(1).toString());
	item->setEditable(false);
	ui.tableView->setRowHeight(row, 18);
	model->setItem(row, 1, item);

	item = new QStandardItem(query.value(2).toString());
	item->setEditable(false);
	ui.tableView->setRowHeight(row, 18);
	model->setItem(row, 2, item);

	item = new QStandardItem(ui.lineEdit_3->text());
	item->setEditable(false);
	ui.tableView->setRowHeight(row, 18);
	model->setItem(row, 3, item);

	item = new QStandardItem(ui.lineEdit_4->text());
	item->setEditable(false);
	ui.tableView->setRowHeight(row, 18);
	model->setItem(row, 4, item);

	QItemSelectionModel *sm = ui.tableView->selectionModel();
	connect(sm, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
		this, SLOT(selectionChanged(QModelIndex,QModelIndex)));
	ui.tableView->show();

	ui.lineEdit_2->setText("");
	ui.lineEdit_3->setText("");
	ui.lineEdit_4->setText("");
	ui.pushButton_2->setFocus();
	textId = "-1";
}
void QKasaKorekcija::selectionChanged(QModelIndex modelX,QModelIndex modelY)
{
	selRow = modelX.row();
}

void QKasaKorekcija::on_pushButton_6_clicked()
{
	model->removeRow(selRow);
}
void QKasaKorekcija::pressReturn()
{
	if (ui.pushButton->hasFocus())
	{
		on_pushButton_clicked();
	}
	else if (ui.pushButton_2->hasFocus())
	{
		on_pushButton_2_clicked();
	}
	else if (ui.pushButton_3->hasFocus())
	{
		on_pushButton_3_clicked();
	}
	else if (ui.pushButton_6->hasFocus())
	{
		on_pushButton_6_clicked();
	}
}
void QKasaKorekcija::init(int id)
{
	if(id == -1)
	{
		return;
	}

	QSqlQuery query;
	query.prepare("select id, kasa_id, datum from priemnici order by kasa_id desc ");
	query.exec();
	query.next();
	ui.lineEdit->setText(query.value(1).toString());
// 	ui.lineEdit->setText(query.value(1).toString())
// 	ui.lineEdit->setText(query.value(1).toString())


	query.prepare(
		"select sifra, artikal, edm, vlez_kol, komintenti.naziv "
		"from magacin_artikli "
		"	left join komintenti  on magacin_artikli.id_komintent=komintenti.id "
		"	left join artikli on magacin_artikli.id_artikal=artikli.id "
		"	where magacin_artikli.vid_dokument='P' and magacin_artikli.document_id=:id "
		);
	query.bindValue(":id", id);	
	query.exec();
	int  r= query.numRowsAffected();
	int c = 4;
	int row = model->rowCount();
	for (int row = 0; row < r; ++row) 
	{
		query.next();
		for (int column = 0; column < c; ++column) 
		{
			QStandardItem *item = new QStandardItem(query.value(column).toString());
			item->setEditable(false);
			ui.tableView->setRowHeight(row, 18);
			ui.tableView->setColumnWidth(column, 150);
			model->setItem(row, column, item);
		}
	}

	QItemSelectionModel *sm = ui.tableView->selectionModel();
	connect(sm, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
		this, SLOT(selectionChanged(QModelIndex,QModelIndex)));
	ui.tableView->show();
}

void QKasaKorekcija::on_pushButton_5_clicked()
{
	pressEscape();
}

void QKasaKorekcija::on_pushButton_4_clicked()
{
	QMessageBox msgBox;
	msgBox.setText("The document has been inserted.");
	msgBox.setInformativeText("Do you want to save your changes?");
	msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
	msgBox.setDefaultButton(QMessageBox::Ok);
	int ret = msgBox.exec();
	if (ret == QMessageBox::Save )
	{
// 		QSqlQuery query;
// 		query.prepare("insert into artikli (sifra, artikal, edm, ref, kataloski_broj) values(:sifra, :artikal, :edm, :ref, :kb)");
// 		query.bindValue(":sifra",ui.lineEdit_2->text().toInt());
// 		query.bindValue(":artikal",ui.lineEdit_3->text());
// 		query.bindValue(":edm",ui.lineEdit_4->text());
// 		query.bindValue(":ref",ui.lineEdit_5->text());
// 		query.bindValue(":kb",ui.lineEdit_6->text());
// 		if (query.exec())
		{
//			emit succesfulEntryData();
		}
	}
}